package top.yxqz.spring.ts.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.yxqz.spring.ts.dao.BookDao;
import top.yxqz.spring.ts.service.BookService;

/**
 * @author: 余小小
 * @date: 2023/4/18
 */
@Service("bookServicexml")
public class BookServiceImplXML implements BookService {

    @Autowired
    private BookDao dao;

    @Override
    public Integer getPriceByBook(Integer bookId) {
        return dao.getPriceByBook(bookId);
    }

    @Override
    public void updateStock(Integer bookId) {
        dao.updateStock(bookId);
    }

    @Override
    public void updateBalance(Integer userId, Integer price) {
        dao.updateBalance(userId, price);
    }

    /**
     * 买书的具体实现
     *
     * @param userId
     * @param bookId 1.查询图书价格
     *               2.更新图书库存
     *               3.更新用户余额
     */
    @Override
    public void buyBook(Integer userId, Integer bookId) {
        if (userId == null || bookId == null) {
            System.out.println("数据有问题，请检查controller传入的数据");
        } else {
//            查询图书价格
            Integer price = dao.getPriceByBook(bookId);
            //更新图书库存
            dao.updateStock(bookId);
            //更新用户余额
            dao.updateBalance(userId, price);
        }
    }
}
